﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace RailwayReservationWeb.employee
{
    public partial class schedule_add : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
            if (!IsPostBack)
            {
                BindTrain();
                BindStation();
                AutoFillTimes();
            }
        }

        private void AutoFillTimes()
        {
            if (String.IsNullOrWhiteSpace(txtDepTime.Text)) { txtDepTime.Text = "00:00:00"; }
            if (String.IsNullOrWhiteSpace(txtArrTime.Text)) { txtArrTime.Text = "00:00:00"; }
            if (String.IsNullOrWhiteSpace(txtTravelTime.Text)) { txtTravelTime.Text = "00:00:00"; }
        }

        private void BindStation()
        {
            cboOrigin.DataSource = db.Stations.OrderBy(p => p.StationName);
            cboDestination.DataSource = db.Stations.OrderBy(p => p.StationName);
            cboOrigin.DataTextField = "StationName";
            cboOrigin.DataValueField = "StationCode";
            cboDestination.DataTextField = "StationName";
            cboDestination.DataValueField = "StationCode";
            cboOrigin.DataBind();
            cboDestination.DataBind();
        }

        private void BindTrain()
        {
            cboTrain.DataSource = db.Trains.OrderBy(p => p.TrainName);
            cboTrain.DataTextField = "TrainName";
            cboTrain.DataValueField = "TrainNo";
            cboTrain.DataBind();
        }

        public string GetRunDays()
        {
            var sb = new StringBuilder();
            for (int i = 0; i < 7; i++)
            {
                if (chlRunDays.Items[i].Selected == true)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
            }
            return sb.ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    AutoFillTimes();
                    var ob = new Schedule();
                    ob.TrainNo = Int32.Parse(cboTrain.SelectedValue.Trim());
                    ob.Origin = cboOrigin.SelectedValue.Trim();
                    ob.Destination = cboDestination.SelectedValue.Trim();
                    ob.DepTime = TimeSpan.Parse(txtDepTime.Text.Trim());
                    ob.ArrTime = TimeSpan.Parse(txtArrTime.Text.Trim());
                    ob.TravelTime = TimeSpan.Parse(txtTravelTime.Text.Trim());
                    ob.Distance = Double.Parse(txtDistance.Text.Trim());
                    ob.RunDays = GetRunDays();
                    ob.Status = 1;
                    db.Schedules.InsertOnSubmit(ob);
                    db.SubmitChanges();
                    Response.Redirect("~/manager/schedule_list.aspx");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }
    }
}